package dao;

import beans.Venda;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class VendaDAO {
    
    PreparedStatement pst;
    ResultSet rs;
    
    public VendaDAO() {
    }
    
    public Venda load(int id) {
        Venda vnd = new Venda();

        String query = "SELECT * FROM venda WHERE id = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, id);

            rs = pst.executeQuery();

            if (rs.next()) {
                vnd.setId(rs.getInt("id"));
                vnd.setData(rs.getDate("data"));
                vnd.setPrecoKg(rs.getFloat("precoKg"));
                vnd.setNotafiscal(rs.getInt("notafiscal"));
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                            ex.printStackTrace();
            }
        }

        return vnd;
    }
    
    public void insert(Venda vnd) {
        String query = "INSERT INTO venda (id, data, precoKg, notafiscal)"
                + " VALUES(?, ?, ?, ?)";

        try {
            pst = Conexao.getConexao().prepareStatement(query);

            pst.setInt(1, vnd.getId());
            pst.setDate(2, vnd.getData());
            pst.setFloat(3, vnd.getPrecoKg());
            pst.setInt(4, vnd.getNotafiscal());

            pst.executeUpdate();

        } catch (Exception ex) {
                                        ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                            ex.printStackTrace();
            }
        }
    }
    
    private void update(Venda vnd) {
        String query = "UPDATE venda"
                + "SET data = ?, precoKg = ?, notafiscal = ?"
                + "WHERE id = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);

            pst.setDate(1, vnd.getData());
            pst.setFloat(2, vnd.getPrecoKg());
            pst.setInt(3, vnd.getNotafiscal());
            pst.setInt(4, vnd.getId());

            pst.executeUpdate();

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }


    }
    
     public void save(Venda vnd) {
        String query = "SELECT COUNT(*) FROM venda WHERE id = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, vnd.getId());
            
            rs = pst.executeQuery();
            rs.next();

            if (rs.getInt("count") != 0) {
                insert(vnd);
            }

        } catch (SQLException ex) {
            ex.printStackTrace();

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }
    }
     
     public void delete(Venda vnd) {
        String query = "DELETE FROM venda WHERE id = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, vnd.getId());

            pst.executeUpdate();

        } catch (SQLException ex) {
            ex.printStackTrace();

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }
    }
     
     public ArrayList<Venda> loadAll() {
        ArrayList<Venda> vendas = new ArrayList<Venda>();

        String query = "SELECT * FROM venda";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            rs = pst.executeQuery();

            while (rs.next()) {
                Venda vnd = new Venda();

                vnd.setId(rs.getInt("id"));
                vnd.setData(rs.getDate("data"));
                vnd.setPrecoKg(rs.getFloat("precoKg"));
                vnd.setNotafiscal(rs.getInt("notafiscal"));

                vendas.add(vnd);
            }
        } catch (SQLException ex) {
            ex.getLocalizedMessage();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }

        return vendas;
    }
}
